package com.lx.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.SessionFactory;
import org.hibernate.loader.custom.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.lx.biz.IResourcebiz;
import com.lx.biz.IUser;
import com.lx.domain.AdminUser;
import com.lx.domain.DataGrid;
import com.lx.domain.SharingResource;

@Controller
public class ManageController {
	
	
	@Resource 
	private IResourcebiz iResourceImpl;
	
	
	@RequestMapping("admin/main")
	public String main(HttpServletRequest httpServletRequest ){
		HttpSession session = httpServletRequest.getSession(true);// 鏀剧疆鍒涘缓session
		AdminUser user = (AdminUser)session.getAttribute("user") ;
		if (user != null&& user.getRole()==99) {
			return "admin/main" ;
		}else if (user != null&& user.getRole()<99) {
			return "admin/main" ;
		} else {
			return "redirect:../login" ;
		}
		
}

	@RequestMapping("admin/resource_datagrid")
	@ResponseBody
	public DataGrid resource_datagrid(String rows ,String page , String sort ,String order ,String ResourceName ,String date_from ,String date_to ){
		List<SharingResource> list = iResourceImpl.listSharingResource(rows,page,sort,order,ResourceName,date_from,date_to);
		int count = iResourceImpl.countResource(rows, page, sort, order, ResourceName, date_from, date_to);
		DataGrid  dg = new DataGrid() ;
        dg.setRows(list);
        dg.setTotal(count);
		return dg ;
	}
	
	@RequestMapping("admin/res_manage")
	public String res_manage(HttpServletRequest httpServletRequest ){
		HttpSession session = httpServletRequest.getSession(true);// 鏀剧疆鍒涘缓session
		AdminUser user = (AdminUser)session.getAttribute("user") ;
		if (user != null&& user.getRole()==99) {
			return "admin/res_manage" ;
		}else if (user != null&& user.getRole()<99) {
			return "admin/res_manage" ;
		} else {
			return "redirect:../login" ;
		}
    }
	
		
	//删除用户
	@RequestMapping("admin/resource/delete")
	@ResponseBody	
	public int delResource(String ids){
		return iResourceImpl.delResource(ids);
	}
	
	@RequestMapping("admin/resource/upload")
	@ResponseBody
	public String redourceAdd(SharingResource sharingResource,MultipartFile resfile,HttpServletRequest request){
		   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd") ;
		    if(resfile==null){
		    	iResourceImpl.resourceAdd(sharingResource) ;
		    	return "success" ;
		    }else {
				String path = request.getSession().getServletContext().getRealPath("upload") ;
				String sonPathString= null ;
				switch (sharingResource.getResourceType()) {
				case "W":
					sonPathString = "windows" ;
					break;
				case "U":
					sonPathString = "ubuntu" ;
					break;
				case "D":
					sonPathString = "document" ;
					break;
				case "O":
					sonPathString = "other" ;
					break;	
				}
				String realUploadPath = path + File.separator +sonPathString ; 
				String filename = df.format(new Date()) +"_"+ resfile.getOriginalFilename() ;     //获取上传文件的名字
				File file = new File(realUploadPath+File.separator +filename) ;
				try {     
		            resfile.transferTo(file); 
		            sharingResource.setResourceUrl("/"+sonPathString+"/"+ filename);
		            iResourceImpl.resourceAdd(sharingResource) ;
		            return "success" ;
		        } catch (IllegalStateException e) {     
		            e.printStackTrace(); 
		            return "faile" ;
		        } catch (IOException e) {            
		            e.printStackTrace(); 
		            return "faile" ;
		        }     
			}
		    
			
	}
	
	@RequestMapping("resAllList")
	public String resListW(String rows , String ResourceType ,HttpServletRequest httpServletRequest){
		List<SharingResource> resources_wList = iResourceImpl.resListType("15", "W") ;
		List<SharingResource> resources_uList = iResourceImpl.resListType("15", "U") ;
		/*List<SharingResource> resources_dList = iResourceImpl.resListType("15", "D") ;
		List<SharingResource> resources_oList = iResourceImpl.resListType("15", "O") ;*/
		httpServletRequest.setAttribute("resources_wList",resources_wList);
		httpServletRequest.setAttribute("resources_uList",resources_uList);
		/*httpServletRequest.setAttribute("resources_dList",resources_dList);
		httpServletRequest.setAttribute("resources_oList",resources_oList);*/
		 return "resAllList" ;
	}
	
	
	@RequestMapping("resource")
	public String fingById(String id ,HttpServletRequest httpServletRequest){
		httpServletRequest.setAttribute("sharingResource", iResourceImpl.findById(id) );
		return "resource" ;
	}
	
	@RequestMapping("resourcelist")
	public String countByNameYype(String ResourceName ,String ResourceType ,HttpServletRequest httpServletRequest){
		httpServletRequest.setAttribute("resourceCount", iResourceImpl.countByNameType(ResourceName, ResourceType) );
		return "resourcelist" ;
	}
	
	@RequestMapping("resource_search")
	public String fingByNameYype(String ResourceName ,String ResourceType ,int pageIndex ,int pageSize ,HttpServletRequest httpServletRequest){
		List<SharingResource> list =iResourceImpl.findResourceByNameType(ResourceName, ResourceType, pageIndex, pageSize) ;
		httpServletRequest.setAttribute("resourceCount", iResourceImpl.countByNameType(ResourceName, ResourceType) );
		httpServletRequest.setAttribute("resourcelist", list);
		return "resource_search" ;
	}
}	